package com.xudow.app.dynamicstate_old.module.user;

import android.support.annotation.NonNull;

import com.jude.beam.expansion.list.BeamListFragmentPresenter;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;

/**
 * Created by ske on 2016/6/10.
 */
public class UserDynamicPresenter extends BeamListFragmentPresenter<UserDynamicFragment, Dynamic> {
    long userId;
    long lastTime;
    @Override
    protected void onCreateView(@NonNull UserDynamicFragment view) {
        super.onCreateView(view);
        userId = getView().getArguments().getLong("userId");
        onRefresh();
    }


    @Override
    public void onRefresh() {
            DynamicModel.getInstance()
                    .getDynamicsForUser(userId, 10, 0, 1)
                    .doOnNext(list -> {
                        if (list!=null&&list.size()>0)
                            lastTime = list.get(list.size()-1).getPublishTime();
                    })
                    .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                    .unsafeSubscribe(getRefreshSubscriber());
    }

    @Override
    public void onLoadMore() {
            DynamicModel.getInstance()
                    .getDynamicsForUser(userId, 10, lastTime, 2)
                    .doOnNext(list -> {
                        if (list!=null&&list.size()>0)
                            lastTime = list.get(list.size()-1).getPublishTime();
                    })
                    .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                    .unsafeSubscribe(getMoreSubscriber());
    }
}
